/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.crm.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import queenb.net.entitybean.K100;
import queenb.net.entitybean.N100;
import queenb.net.entitybean.Q400;
import queenb.web.controller.QueenbController;
import queenb.web.crm.store.CustomerStore;
import queenb.web.crm.store.K100Store;
import queenb.web.crm.store.K400Store;
import queenb.web.store.EmployeeStore;
import queenb.web.util.SessionProvider;

/**
 *
 * @author VANANH
 */
public class K400Controller extends QueenbController {

    private int PK400;
    private int FN600;
    private int FN450;
    private int Fn850;
    private String KV401 = "Nhập tên nhóm";
    private String KV402;
    private List<N100> list = new ArrayList<N100>();
    private List<K100> lstK100 = new ArrayList<K100>();
    private List<K100> lstK100Re = new ArrayList<K100>();
    private List<N100> listExpand = new ArrayList<N100>();
    private List<K100> listCompanyAll = new ArrayList<K100>();
    private List<N100> listremove = new ArrayList<N100>();
    private String keysearch = "Nhập từ khóa";
    private int sessionPK400;
    private List<N100> listtour = new ArrayList<N100>();
    private int pn100;
    private int pk100;
    private List<Q400> listQ400;
    private boolean flagUpdate;
    private String messagefrom;
    private int beginContructor;
    private String pageload;
    private String message;
    private String kv451;
    private EmployeeStore empStore;
    private CustomerStore cusStore;
    private K100Store k100Store;
    private int type = 0;
    private int days = 0;

    public K400Controller() throws SQLException, InstantiationException {
        cusStore = new CustomerStore();
        k100Store = new K100Store();
        Object objPN100 = getRequest().getParameter("pn100");
        Object objPK100 = getRequest().getParameter("pk100");
        Object objKV451 = getRequest().getParameter("kv451");
        Object objPk400 = getRequest().getParameter("pk400");
        Object objAction = getRequest().getParameter("action");
        Object objFN450 = getRequest().getParameter("fn450");
        Object objFN850 = getRequest().getParameter("fn450");
        Object objPage = getRequest().getParameter("page");
        Object objType = getRequest().getParameter("type");
        Object objDayss = getRequest().getParameter("dayss");
        System.out.println("K400Controller>>" + objPN100 + "--" + objPk400);
        if (objPk400 != null) {
            PK400 = Integer.parseInt(objPk400.toString());
        }
        if (objType != null) {
            type = Integer.parseInt(objType.toString());
        }
        if (objDayss != null) {
            days = Integer.parseInt(objDayss.toString());
        }
        if (objPK100 != null) {
            pk100 = Integer.parseInt(objPK100.toString());
        }
        if (objPN100 != null) {
            this.pn100 = Integer.parseInt(objPN100.toString());
        }
        if (objAction != null) {
            if (objAction.equals("updateK400N")) {
                flagUpdate = true;
                updateK400();
            } else if (objAction.equals("updateK400K")) {
                flagUpdate = false;
                updateK400();
            } else if (objAction.equals("checkGroup")) {
                if (objPk400 != null) {
                    PK400 = Integer.parseInt(objPk400.toString());
                }
                if (objKV451 != null) {
                    kv451 = objKV451.toString();
                }
                if (objPN100 != null) {
                    pn100 = Integer.parseInt(objPN100.toString());

                    saveSelected(PK400, pn100, kv451);
                }
                if (objPk400 != null) {
                }
            } else if (objAction.equals("unCheckGroup")) {
                if (objPk400 != null) {
                    PK400 = Integer.parseInt(objPk400.toString());
                }
                if (objKV451 != null) {
                    kv451 = objKV451.toString();
                }
                if (objPN100 != null) {
                    pn100 = Integer.parseInt(objPN100.toString());
                }
                deleteSelected(PK400, pn100, kv451);

            } else if (objAction.equals("loadGroup")) {
                if (objPage != null) {
                    pageload = objPage.toString();
                }
                if (objPk400 != null) {
                    PK400 = Integer.parseInt(objPk400.toString());
                }
                if (objKV451 != null) {
                    kv451 = objKV451.toString();
                }
                if (objFN850 != null) {
                    Fn850 = Integer.parseInt(objFN850.toString());
                }
                if (objFN450 != null) {
                    FN450 = Integer.parseInt(objFN450.toString());
                }
            }
        }


    }

    public void reload() {
        System.out.println("---change+++++++++++++" + keysearch);
    }

    private int saveK400(String kv402) {
        int rs = 0;
        try {
            System.out.println("---saveK400+++++++++++++" + KV401 + "---kv402--" + kv402);
            K400Store store = new K400Store();
            rs = store.updateK400(PK400, KV401, kv402, getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getId(), getLogin().getUserName());
        } catch (Exception e) {
            rs = -1;
        }
        return rs;

    }

    private void showMesg(int rs) {

        if (rs > 0) {
            showGlobalReport(true, getResourceString("datasavesuccessful"));
        } else {
            showGlobalReport(true, getResourceString("datasuccessfail"));
        }
    }

    public void saveK400forN100E() {
        showMesg(saveK400("N100E"));
        sendRedirect("employee.jsp");
    }

    public void saveK400forK100() {
        showMesg(saveK400("K100"));
        sendRedirect("cuscompany.jsp");
    }
    
    public void saveK400forK100S() {
        showMesg(saveK400("K100S"));
        sendRedirect("supplier.jsp");
    }
    
    public void saveK400forN100C() {
        showMesg(saveK400("N100C"));
        sendRedirect("customer.jsp");
    }

//    public void saveSelected() {      
//        K400Store store = new K400Store();
//        for (int i = 0; i < list.size(); i++) {         
//            try {
//                if (list.get(i).isSelected()) {
//                    store.updateK450(getSessionPK400(),list.get(i).getPn100(), "N100", null, getLogin().getUserName());
//                }
//            } catch (Exception ex)" {
//            }
//        }
//    }
    // them khách hàng vào nhóm
    private void saveSelected(int pk400, int pn100, String kv451) {
        K400Store store = new K400Store();
        try {
            int count = 0;
            System.out.println("------------ Thêm vào===" + pk400 + "," + pn100 + "," + kv451);
            count = store.updateK450(pk400, pn100, kv451, null, getLogin().getUserName());
            System.out.println("------------ Thêm vào===" + count);
            if (count > 0) {
                setMessage("CẬP NHẬT THÀNH CÔNG!");
            } else {
                setMessage("CẬP NHẬT KHÔNG THÀNH CÔNG!");
            }
        } catch (Exception e) {
        }
//        for (int i = 0; i < list.size(); i++) {         
//            try {
//                if (list.get(i).isSelected()) {
//                    store.updateK450(getSessionPK400(),list.get(i).getPn100(), "N100", null, getLogin().getUserName());
//                }
//            } catch (Exception ex) {
//            }
//        }
    }

    private void deleteSelected(int pk400, int pn100, String kv451) {
        K400Store store = new K400Store();

        try {
            int count = 0;
            System.out.println("------------deleteSelected--" + pk400 + "," + pn100 + "," + kv451);
            count = store.deleteK450(pk400, pn100, kv451, getLogin().getUserName());
            System.out.println("------------deleteSelected" + count);
            if (count > 0) {
                setMessage("CẬP NHẬT THÀNH CÔNG!");
            } else {
                setMessage("CẬP NHẬT KHÔNG THÀNH CÔNG!");
            }

        } catch (Exception ex) {
        }


    }

    public List<Q400> getListGroupQ400() throws SQLException, InstantiationException {
        try {
            K400Store k400Store = new K400Store();
            if (getListQ400() == null) {
                setListQ400(k400Store.CombK400(getLogin().getModule(), getLogin().getCompanyNo(), 0, getLogin().getUserName()));
            }

            return getListQ400();
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CustomerController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public void updateK400() throws SQLException, InstantiationException {
        System.out.println("---group+++++++++pk400++++>>>>>>>>" + pn100 + "--" + PK400 + "---" + flagUpdate);

        if (flagUpdate == false) {
            try {
                K400Store store = new K400Store();
                store.updateK450(PK400, pk100, "K100", null, getLogin().getUserName());
                sendRedirect("cuscompany.jsp");
                showGlobalReport(true, getResourceString("datasavesuccessful"));

            } catch (IllegalAccessException ex) {

                java.util.logging.Logger.getLogger(CustomerController.class.getName()).log(Level.SEVERE, null, ex);
                sendRedirect("cuscompany.jsp");
                showGlobalReport(true, getResourceString("datasuccessfail"));
            }
        } else {

            try {
                K400Store store = new K400Store();
                store.updateK450(PK400, pn100, "N100", null, getLogin().getUserName());
                sendRedirect("customer.jsp");
                showGlobalReport(true, getResourceString("datasavesuccessful"));

            } catch (IllegalAccessException ex) {

                java.util.logging.Logger.getLogger(CustomerController.class.getName()).log(Level.SEVERE, null, ex);
                sendRedirect("customer.jsp");
                showGlobalReport(true, getResourceString("datasuccessfail"));
            }
        }







    }

    /**
     * @return the PK400
     */
    public int getPK400() {
        return PK400;
    }

    /**
     * @param PK400 the PK400 to set
     */
    public void setPK400(int PK400) {
        this.PK400 = PK400;
    }

    /**
     * @return the KV401
     */
    public String getKV401() {
        return KV401;
    }

    /**
     * @param KV401 the KV401 to set
     */
    public void setKV401(String KV401) {
        this.KV401 = KV401;
    }

    /**
     * @return the KV402
     */
    public String getKV402() {
        return KV402;
    }

    /**
     * @param KV402 the KV402 to set
     */
    public void setKV402(String KV402) {
        this.KV402 = KV402;
    }

    /**
     * @return the list
     */
    public List<N100> getList() {

        CustomerStore store = new CustomerStore();
        if (keysearch.contains("Nhập từ khóa")) {
            keysearch = "";
        }
        try {
            //fk600 nhan vien = 1, cus = 2
            //kv401 nv n100E, khach hang n100c, cty k100
            if (pageload.equals("customer.jsp")) {
                FN600 = 2;
                kv451 = "N100C";
            } else if (pageload.equals("employee.jsp")) {
                FN600 = 1;
                kv451 = "N100E";
            }
            System.out.println("getList--" + pageload + "-FN450-" + FN450 + "-Fn850-" + Fn850 + "," + "-PK400-" + PK400 + "-kv451-" + kv451);
            list = store.getListCustomerNoGroup(getLogin().getModule(), getLogin().getCompanyNo(), getFN600(), FN450, Fn850, PK400, kv451,keysearch, 0, 1000, 0, 1, getLogin().getUserName());
            // list = store.searchListCustomer(getLogin().getModule(), 99, getSessionPK400(), getLogin().getCharacter(), keysearch, getLogin().getUserName());
            System.out.println("aaaaaaaaa size" + list.size() + PK400 + "--" + getLogin().getCharacter() + "--" + keysearch);
        } catch (Exception e) {
        }

        return list;
    }

    /**
     * @param list the list to set
     */
    public void setList(List<N100> list) {


        this.list = list;
    }

    /**
     * @return the listremove
     */
    public List<N100> getListremove() {
        CustomerStore store = new CustomerStore();
        if (keysearch.contains("Nhập từ khóa")) {
            keysearch = "";
        }
        System.out.println("getListremove--" + pageload + "-FN450-" + FN450 + "-Fn850-" + Fn850 + "," + "-PK400-" + PK400 + "--keysearch--" + keysearch);
        try {
            if (pageload.equals("customer.jsp")) {
                listremove = store.removeListCustomer(getLogin().getModule(), getLogin().getCompanyNo(), PK400, "N100C", keysearch, getLogin().getUserName());
            } else if (pageload.equals("employee.jsp")) {
                listremove = store.removeListCustomer(getLogin().getModule(), getLogin().getCompanyNo(), PK400, "N100E", keysearch, getLogin().getUserName());
            }


            System.out.println("list remove***" + "--" + listremove.size());
        } catch (Exception e) {
        }

        return listremove;
    }

    /**
     * @param listremove the listremove to set
     */
    public void setListremove(List<N100> listremove) {
        this.listremove = listremove;
    }

    /**
     * @return the keysearch
     */
    public String getKeysearch() {
        return keysearch;
    }

    /**
     * @param keysearch the keysearch to set
     */
    public void setKeysearch(String keysearch) {
        this.keysearch = keysearch;
    }

    /**
     * @return the sessionPK400
     */
    public int getSessionPK400() {

        if (SessionProvider.getSessionMapValue("groupcustomerID") == null) {
            SessionProvider.setSessionMapValue("groupcustomerID", 0);
        }
        sessionPK400 = Integer.parseInt(SessionProvider.getSessionMapValue("groupcustomerID").toString());
        return sessionPK400;
    }

    /**
     * @param sessionPK400 the sessionPK400 to set
     */
    public void setSessionPK400(int sessionPK400) {
        this.sessionPK400 = sessionPK400;
    }

    /**
     * @return the listtour
     */
    public List<N100> getListtour() {
        CustomerStore store = new CustomerStore();
        listtour = store.TourHistory(getLogin().getModule(), getLogin().getCompanyNo(), pn100, getLogin().getUserName());
        System.out.println("aaaaaaaaa size" + listtour.size());
        return listtour;
    }

    /**
     * @param listtour the listtour to set
     */
    public void setListtour(List<N100> listtour) {
        this.listtour = listtour;
    }

    /**
     * @return the pn100
     */
    public int getPn100() {
        return pn100;
    }

    /**
     * @param pn100 the pn100 to set
     */
    public void setPn100(int pn100) {
        this.pn100 = pn100;
    }

    /**
     * @return the listQ400
     */
    public List<Q400> getListQ400() {
        return listQ400;
    }

    /**
     * @param listQ400 the listQ400 to set
     */
    public void setListQ400(List<Q400> listQ400) {
        this.listQ400 = listQ400;
    }

    /**
     * @return the pk100
     */
    public int getPk100() {
        return pk100;
    }

    /**
     * @param pk100 the pk100 to set
     */
    public void setPk100(int pk100) {
        this.pk100 = pk100;
    }

    /**
     * @return the flagUpdate
     */
    public boolean isFlagUpdate() {
        return flagUpdate;
    }

    /**
     * @param flagUpdate the flagUpdate to set
     */
    public void setFlagUpdate(boolean flagUpdate) {
        this.flagUpdate = flagUpdate;
    }

    public void saveSelected1() {
        int id = 0;
        // System.out.println("---select+++++++++++++" + SessionProvider.getSessionMapValue("PKKKK"));
//        if (SessionProvider.getSessionMapValue("PKKKK") == null || SessionProvider.getSessionMapValue("PKKKK").equals("")) {
//            id = Integer.parseInt(SessionProvider.getSessionMapValue("PK100SS").toString());
//        }
//        else
//        {
//            id = Integer.parseInt(SessionProvider.getSessionMapValue("PKKKK").toString());
//        }

        Object objFK100 = getRequest().getParameter("PK100");
        if (objFK100 != null) {
            this.pk100 = Integer.parseInt(objFK100.toString());
        }
        CustomerStore store = new CustomerStore();
        for (int i = 0; i < listExpand.size(); i++) {
            System.out.println("---select+++++++++++++" + listExpand.get(i).isSelected() + "pk100---" + pk100);
            try {
                if (listExpand.get(i).isSelected()) {
                    int rs = store.updateColumnINT(null, "N100", "FK100", pk100, "PN100", listExpand.get(i).getPn100(), getLogin().getUserName());
                    if (rs != 0) {
                        setMessagefrom(getResourceString("success"));
                    } else {
                        setMessagefrom(getResourceString("error"));
                    }
                }
            } catch (Exception ex) {
            }

        }
    }

    public void saveSelected2() {
        int id = 0;
        System.out.println("---select+++++++++++++" + SessionProvider.getSessionMapValue("PKKKK"));
        if (SessionProvider.getSessionMapValue("PNNNN") == null || SessionProvider.getSessionMapValue("PKKKK").equals("")) {
            id = Integer.parseInt(SessionProvider.getSessionMapValue("PK100SS").toString());
        } else {
            id = Integer.parseInt(SessionProvider.getSessionMapValue("PNNNN").toString());
        }


        CustomerStore store = new CustomerStore();
        for (int i = 0; i < listCompanyAll.size(); i++) {
            System.out.println("---select+++++++++++++" + listCompanyAll.get(i).isSelected());
            try {
                if (listCompanyAll.get(i).isSelected()) {
                    int rs = store.updateColumnN100(null, "N100", "PN100", String.valueOf(id), "FK100", listCompanyAll.get(i).getPk100(), getLogin().getUserName());
                }
            } catch (Exception ex) {
            }

        }
    }

    /**
     * @return the listExpand
     */
    public List<N100> getListExpand() {
        if (keysearch.contains("Nhập từ khóa")) {
            keysearch = "";
        }

        EmployeeStore store = new EmployeeStore();
        List<N100> lst = store.getListComN100Name(getLogin().getModule(), getLogin().getCompanyNo(), 0, 0, keysearch, 0, 1, 0, 0, getLogin().getUserName());
        int total = 0;
        if (lst != null && lst.size() > 0) {
            total = lst.get(0).getRowss();
        }
        listExpand = store.getListComN100Name(getLogin().getModule(), getLogin().getCompanyNo(), 0, 0, keysearch, 0, total, 0, 0, getLogin().getUserName());
        System.out.println("List cua Tung" + listExpand.size());

        return listExpand;
    }

    /**
     * @param listExpand the listExpand to set
     */
    public void setListExpand(List<N100> listExpand) {
        this.listExpand = listExpand;
    }

    /**
     * @return the listCompanyAll
     */
    public List<K100> getListCompanyAll() {
        if (keysearch.contains("Nhập từ khóa")) {
            keysearch = "";
        }

        K100Store store = new K100Store();
        listCompanyAll = store.getListCompany_Cus(getLogin().getCompanyNo(), getLogin().getModule(), keysearch, getLogin().getUserName());
        System.out.println("List cong ty trong Dashboard" + listCompanyAll.size());

        return listCompanyAll;
    }

    /**
     * @param listCompanyAll the listCompanyAll to set
     */
    public void setListCompanyAll(List<K100> listCompanyAll) {
        this.listCompanyAll = listCompanyAll;
    }

    /**
     * @return the messagefrom
     */
    public String getMessagefrom() {
        return messagefrom;
    }

    /**
     * @param messagefrom the messagefrom to set
     */
    public void setMessagefrom(String messagefrom) {
        this.messagefrom = messagefrom;
    }

    /**
     * @return the beginContructor
     */
    public int getBeginContructor() {
        return beginContructor;
    }

    /**
     * @param beginContructor the beginContructor to set
     */
    public void setBeginContructor(int beginContructor) {
        this.beginContructor = beginContructor;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the FN600
     */
    public int getFN600() {
        return FN600;
    }

    /**
     * @param FN600 the FN600 to set
     */
    public void setFN600(int FN600) {
        this.FN600 = FN600;
    }

    /**
     * @return the FN450
     */
    public int getFN450() {
        return FN450;
    }

    /**
     * @param FN450 the FN450 to set
     */
    public void setFN450(int FN450) {
        this.FN450 = FN450;
    }

    /**
     * @return the Fn850
     */
    public int getFn850() {
        return Fn850;
    }

    /**
     * @param Fn850 the Fn850 to set
     */
    public void setFn850(int Fn850) {
        this.Fn850 = Fn850;
    }

    /**
     * @return the pageload
     */
    public String getPageload() {
        return pageload;
    }

    /**
     * @param pageload the pageload to set
     */
    public void setPageload(String pageload) {
        this.pageload = pageload;
    }

    /**
     * @return the kv451
     */
    public String getKv451() {
        return kv451;
    }

    /**
     * @param kv451 the kv451 to set
     */
    public void setKv451(String kv451) {
        this.kv451 = kv451;
    }

    /**
     * @return the empStore
     */
    public EmployeeStore getEmpStore() {
        if (empStore == null) {
            empStore = new EmployeeStore();
        }
        return empStore;
    }

    /**
     * @param empStore the empStore to set
     */
    public void setEmpStore(EmployeeStore empStore) {
        this.empStore = empStore;
    }

    /**
     * @return the cusStore
     */
    public CustomerStore getCusStore() {

        return cusStore;
    }

    /**
     * @param cusStore the cusStore to set
     */
    public void setCusStore(CustomerStore cusStore) {
        this.cusStore = cusStore;
    }

    /**
     * @return the k100Store
     */
    public K100Store getK100Store() {

        return k100Store;
    }

    /**
     * @param k100Store the k100Store to set
     */
    public void setK100Store(K100Store k100Store) {
        this.k100Store = k100Store;
    }

    /**
     * @return the lstK100
     */
    public List<K100> getLstK100() {
        try {
            if (keysearch.contains("Nhập từ khóa")) {
                keysearch = "";
            }
            System.out.println("getLstK100--" + pageload + "-FN450-" + FN450 + "-Fn850-" + Fn850 + "," + "-PK400-" + PK400 + "-keysearch-" + keysearch);
            lstK100 = k100Store.LISTOFTABK100NO(getLogin().getModule(), getLogin().getCompanyNo(), PK400, "K100", keysearch, getLogin().getUserName());
            System.out.println("getLstK100--" + lstK100.size());
            return lstK100;

        } catch (Exception e) {
            return new ArrayList<K100>();
        }

    }

    /**
     * @param lstK100 the lstK100 to set
     */
    public void setLstK100(List<K100> lstK100) {
        this.lstK100 = lstK100;
    }

    /**
     * @return the lstK100Re
     */
    public List<K100> getLstK100Re() {
        try {
            if (keysearch.contains("Nhập từ khóa")) {
                keysearch = "";
            }
            System.out.println("getLstK100Re--" + pageload + "-FN450-" + FN450 + "-Fn850-" + Fn850 + "," + "-PK400-" + PK400 + "-keysearch-" + keysearch);

            lstK100Re = k100Store.LISTOFTABK100EX(getLogin().getModule(), getLogin().getCompanyNo(), PK400, "K100", keysearch, getLogin().getUserName());
            System.out.println("getLstK100Re--" + lstK100Re.size());
            return lstK100Re;

        } catch (Exception e) {
            return new ArrayList<K100>();
        }
    }

    /**
     * @param lstK100Re the lstK100Re to set
     */
    public void setLstK100Re(List<K100> lstK100Re) {
        this.lstK100Re = lstK100Re;
    }

    /**
     * @return the type
     */
    public int getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * @return the days
     */
    public int getDays() {
        return days;
    }

    /**
     * @param days the days to set
     */
    public void setDays(int days) {
        this.days = days;
    }
}
